<script setup>
import {
  Edit,
  Delete, User,CircleCloseFilled
} from '@element-plus/icons-vue'
import { ref } from 'vue'
const value1 = ref('');
const pickerOptions = ref({
  disabledDate(time) {
    return time.getTime() > Date.now();
  },
});
const supplier = ref([
  {   "id":7,
    "supplierName":"钱大妈",
    "supplierAddress":"丽萨天下无敌萨迦天地总歇会",
    "supplierPhone":"1234678",
    "supplierEmail":"3526279811@qq.com",
    "postalCode":"Sc122323",
    "contactPerson":"我当时",
    "contactPhone":"21312412"
    ,"bankName":"天地银行",
    "bankAccount":"1312315",
    "status":"可用"
  }

])
const products = ref([
  {   "id":7,
    "productName":"钱大妈",
    "supplierId":1,
    "productDescription":"fs社",
    "sellingPrice":12,
    "productQuantity":120,
    "productSpecification":"12个",
    "packagingUnit":"个",
    "batchNumber":213412,
    "approvalNumber":"213122",
    "status":"可用"
  }

])
const rePurchases = ref([
  { "id":1,
    "productId":1,
    "productName":'桑克斯',
    "supplierId":1,
    "supplierName":'先天',
    "paymentType":"支付宝",
    "purchaseTime":"2024-06-30 14:30:50",
    "operatorId":1,
    "operatorName":'管理员0',
    "purchaseQuantity":3,
    "productSpec":"12袋/个",
    "purchasePrice":10,
    "totalPrice":30,
    "remark":""}
])
const manger = ref([
  {     "id": 6,
    "username": "管理员2",
    "nickname": "lwta7",
    "email": "3526279811@qq.com",
    "createTime": "2024-06-28T15:50:55",
    "updateTime": "2024-06-28T15:50:55"
  }
])
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(3)//每页条数
const onSizeChange = (size) => {
  pageSize.value = size
  purchaseList();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  purchaseList();
}
import {supplierListService}from '@/api/supplier.js'
import {productListService} from "@/api/product.js";
import  {AddPurchaseService,purchaseListService,returnPurchaseService,deletePurchaseService,updatePurchaseService} from '@/api/purchase.js'
import {mangerListService} from "@/api/manger.js";
const supplierList = async ()=>{
  let  params1 = {
    pageNum: 1,
    pageSize:100,
  }
  let result =  await  supplierListService(params1);
  total.value =  result.data.total
  supplier.value = result.data.items;
}
supplierList();
const productList = async ()=>{
  let  params = {
    pageNum: 1,
    pageSize:100,
  }
  let result =  await  productListService(params);
  total.value =  result.data.total
  products.value = result.data.items;
}
productList();
const mangerList =async ()=>{
  let  params2 = {
    pageNum: 1,
    pageSize:100,
  }
  let result =  await  mangerListService(params2);
  total.value =  result.data.total
  manger.value = result.data.items;
}
mangerList();
const supplierId = ref('')
const  productId = ref('')
const purchaseList = async ()=>{
  let params ={
    pageNum:pageNum.value,
    pageSize:pageSize.value,
    supplierId:supplierId.value?supplierId.value:null,
    productId:productId.value?productId.value:null,
  }
  let result = await purchaseListService(params);
  total.value =  result.data.total
  rePurchases.value = result.data.items;
  for (let i = 0; i < rePurchases.value.length; i++) {
    let rePurchase = rePurchases.value[i]
    for (let j = 0; j < supplier.value.length; j++) {
      if (rePurchase.supplierId === supplier.value[j].id) {
        rePurchase.supplierName = supplier.value[j].supplierName
      }
    }
  }
  for (let i = 0; i < rePurchases.value.length; i++) {
    let rePurchase = rePurchases.value[i]
    for (let j = 0; j < products.value.length; j++) {
      if (rePurchase.productId === products.value[j].id) {
        rePurchase.productName = products.value[j].productName
      }
    }
  }
  for (let i = 0; i < rePurchases.value.length; i++) {
    let rePurchase = rePurchases.value[i]
    for (let j = 0; j < manger.value.length; j++) {
      console.log(manger.value[j])
      if (rePurchase.operatorId === manger.value[j].id) {
        rePurchase.operatorName = manger.value[j].nickname
      }
    }
  }
}
purchaseList();
import {Plus} from '@element-plus/icons-vue'
import {ElMessage, ElMessageBox} from "element-plus";
import {useTokenStore} from "@/Stores/token.js";
import RePurchase from "@/components/purchase/RePurchase.vue";
import {returnSaleService} from "@/api/sale.js";

const visibleDrawer = ref(false)
//添加表单数据模型
const purchaseModel = ref({
  id:'',
  productId: '',
  supplierId: '',
  paymentType: '',
  purchaseTime:'',
  operatorId:'',
  purchaseQuantity:'',
  productSpec:'',
  purchasePrice:'',
  totalPrice:'',
  remark:''
})
const title =ref('')
const tokenStore = useTokenStore()
const updatePurchaseEcho =  (row)=>{
  title.value = '修改进货'
  visibleDrawer.value = true
  purchaseModel.value.id = row.id
  purchaseModel.value.purchaseTime = row.purchaseTime
  purchaseModel.value.productId = row.productId
  purchaseModel.value.supplierId = row.supplierId
  purchaseModel.value.paymentType = row.paymentType
  purchaseModel.value.operatorId = row.operatorId
  purchaseModel.value.purchaseQuantity = row.purchaseQuantity
  purchaseModel.value.productSpec = row.productSpec
  purchaseModel.value.purchasePrice = row.purchasePrice
  purchaseModel.value.totalPrice = row.totalPrice
  purchaseModel.value.remark = row.remark
}
const returnPurchaseEchoId =(row) => {
  dialogVisible.value = true;
  purchaseModel.value.id = row.id
}
const updatePurchase = async ()=>{
  let result =  await updatePurchaseService(purchaseModel.value);
  ElMessage.success(result.data?result.data:'修改成功')
  purchaseList()
  visibleDrawer.value = false
}
const clearData = ()=>{
  purchaseModel.value.id = ''
  purchaseModel.value.purchaseTime = ''
  purchaseModel.value.productId = ''
  purchaseModel.value.supplierId =''
  purchaseModel.value.paymentType = ''
  purchaseModel.value.operatorId = ''
  purchaseModel.value.purchaseQuantity = ''
  purchaseModel.value.productSpec = ''
  purchaseModel.value.purchasePrice = ''
  purchaseModel.value.totalPrice = ''
  purchaseModel.value.remark = ''
}
const addPurchase = async ()=> {
  let result = await AddPurchaseService(purchaseModel.value)
  ElMessage.success(result.data?result.data:'添加成功')
  purchaseList()
  visibleDrawer.value = false
}
const deletePurchase = (row) => {
  ElMessageBox.confirm(
      '确认删除该条退货吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let result = await deletePurchaseService(row.id);
        ElMessage({
          type: 'success',
          message: '确认删除',
        })
        purchaseList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消了删除',
        })
      })
}
const dialogVisible = ref(false)
const reNum = ref(0)
const rePurchase = async ()=>{
  let  params = {
    id:purchaseModel.value.id?purchaseModel.value.id:null,
    reNum :reNum.value
  }
  let result =  await  returnPurchaseService(params);
  purchaseList()
  dialogVisible.value=false
}
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>进货列表</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer=true;title='添加进货';clearData()">添加进货</el-button>
        </div>
      </div>
    </template>
    <el-form inline>
      <el-form-item label="供应商名称:">
        <el-select placeholder="请选择供应商名称" v-model="supplierId" style="width: 180px">
          <el-option
              v-for="c in supplier"
              :key="c.id"
              :label="c.supplierName"
              :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称:">
        <el-select placeholder="请选择商品名称" v-model="productId" style="width: 180px">
          <el-option
              v-for="c in products"
              :key="c.id"
              :label="c.productName"
              :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="purchaseList">搜索</el-button>
        <el-button @click="supplierId='';productId='';purchaseList()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="rePurchases" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="图片">
        <template #default="scope">
          <img src="/images/01.jpg" alt="" style="width: 100%; height: auto; display: block;">
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="productName"></el-table-column>
      <el-table-column label="供应商名称" prop="supplierName"></el-table-column>
      <el-table-column label="支付类型" prop="paymentType"></el-table-column>
      <el-table-column label="进货时间" prop="purchaseTime"></el-table-column>
      <el-table-column label="操作人" prop="operatorName"></el-table-column>
      <el-table-column label="商品数量" prop="purchaseQuantity"></el-table-column>
      <el-table-column label="包装规格" prop="productSpec"></el-table-column>
      <el-table-column label="商品价格" prop="purchasePrice"></el-table-column>
      <el-table-column label="商品总价" prop="totalPrice"></el-table-column>
      <el-table-column label="备注" prop="remark"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="updatePurchaseEcho(row)" ></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deletePurchase(row)"></el-button>
          <el-button :icon="CircleCloseFilled" @click="returnPurchaseEchoId(row)" circle plain type="primary"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />

    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <!-- 添加文章表单 -->
      <el-form :model="purchaseModel" label-width="100px" >
        <el-form-item label="商品名称" >
          <el-select placeholder="请选择商品名称" v-model="purchaseModel.productId" style="width: 180px">
            <el-option
                v-for="c in products"
                :key="c.id"
                :label="c.productName"
                :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商名称" >
          <el-select placeholder="请选择供应商名称" v-model="purchaseModel.supplierId" style="width: 180px">
            <el-option
                v-for="c in supplier"
                :key="c.id"
                :label="c.supplierName"
                :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付类型" >
          <el-input v-model="purchaseModel.paymentType" placeholder="请输入支付类型"></el-input>
        </el-form-item>
          <el-form-item label="进货时间">
            <el-date-picker
                v-model="value1"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
            ></el-date-picker>
          </el-form-item>
        <el-form-item label="进货数量" >
          <el-input v-model="purchaseModel.purchaseQuantity" placeholder="请输入商品数量"></el-input>
        </el-form-item>
        <el-form-item label="进货价格" >
          <el-input v-model="purchaseModel.purchasePrice" placeholder="请输入进货价格"></el-input>
        </el-form-item>
        <el-form-item label="备注" >
          <el-input v-model="purchaseModel.remark" placeholder="请输入备注"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="title==='添加进货'?addPurchase():updatePurchase()">提交</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
  <el-dialog v-model="dialogVisible" title="退货" width="30%">
    <el-form label-width="100px" style="padding-right: 30px">
      <el-form-item :model="purchaseModel" label="退货数量" >
        <el-input disabled v-model="purchaseModel.id" placeholder="请输入退货数量"></el-input>
      </el-form-item>
      <el-form-item label="退货数量" >
        <el-input v-model="reNum" placeholder="请输入退货数量"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="rePurchase"> 确认 </el-button>
        </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.page-container {
  margin-left: 5px;
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
